Problem 1

y <- numeric(1000)
y <- seq(1:1000)
for (i in 1:1000) { if (i%%3==0&i%%5==0) y[i] <- "Royal Blood" else {
  if(i%%3 == 0) y[i] <- "Royal" else if(i%%5 == 0) y[i] <- "Blood"}
  }
cat(paste(c(y[1:35],"..."),collapse = ","), "\n")
1,2,Royal,4,Blood,Royal,7,8,Royal,Blood,11,Royal,13,14,Royal Blood,16,17,Royal,19,Blood,Royal,22,23,Royal,Blood,26,Royal,28,29,Royal Blood,31,32,Royal,34,Blood,... 

Problem 2

inscaven (2017)

a <-sample(1:1000,10) # reference 2
a
 [1] 808 121 139 481 488 581 520 357 867 189
orderr <- function(a)
{
  n <- length(a) 
  for (k in n:2) 
  {
    i <- 1       
    while (i < k)
    {
      if (a[i] > a[i+1])
      {
        temp <- a[i+1]
        a[i+1] <- a[i]
        a[i] <- temp
      }
      i <- i+1     
    }
  }
  a   
}

a <- orderr(a)
cat(a)
121 139 189 357 481 488 520 581 808 867

Problem 3

func <- function(x) x^2 - 2*x + 1
set.seed(213)
tol <- 1e-5
diff <- tol +1
x1 <- -1
x3 <- 3
x2 <- runif(1,-1,3)
f_old <- func(x2)
i = 0 
f_new <- 0

while ((diff > tol)|(f_old == f_new)) {
  i= i +1
  set.seed(21+i)
  f_old <- func(x2)
  b = x3-x2
  a = x2-x1
  if (b>a) {
    set.seed(31+i)
    x4 <- runif(1,x2,x3)
    if (func(x4) > func(x2)) {
      x3 <- x4
    } else {
      x1 <- x2
      x2 <- x4
    }
  } else {
    set.seed(24+i)
    x4 <- runif(1,x1,x2)
    if (func(x4) > func(x2)) {
      x1 <- x4
    } else {
      x3 <- x2
      x2 <- x4
    }
  }
  f_new <- func(x2)
  diff <- abs(f_new-f_old)
  i=i+1
  }

xmin <- x2
xmin
[1] 0.9999015

In this algorithm, there are two possible events that can lead to failure. For the first one, in theory of the question mentioned, “given the b is the largest” means if we are going to select x4, we have to have a > b or a < b. Therefore, we have to consider the event a = b in iterations. We can consider two situations, x4 in a or x4 in b, then make a comparison between these two situations, and select the smaller one. By the way, in the solution, this event was not considered.

For the second one, when we use uniform distribution to select x4 randomly, x4 may close to x2 extremely, which can lead to |func(x4)-func(x2)|<1e-5. Then, the iteration process ends. The outcome couold be not the minimum value.

Bonus Problem

Liu (2017) Xiao (2016) Borrelli (2014) You (2019)

getpoint <- function(cx,cy,r,stx,sty,edx,edy){ # calculate the point of intersection between circle and line.
  p <-c(0,0)  # reference 3
  k <- (edy - sty) / (edx - stx)
  b <- edy - k*edx
  c <- cx*cx + (b - cy)*(b- cy) -r*r
  a <- (1 + k*k)
  b1 <- (2*cx - 2*k*(b - cy))
  
  tmp <- sqrt(b1*b1 - 4*a*c)
  x1 <- ( b1 + tmp )/(2*a)
  y1 <- k*x1 + b
  x2 <- ( b1 - tmp)/(2*a)
  y2 <- k*x2 + b
  res <- (x1 -cx)*(x1 -cx) + (y1 - cy)*(y1 -cy);
  l1<-(edx-x1)^2+(edy-y1)^2
  l2<-(edx-x2)^2+(edy-y2)^2
  if (l1>l2) {
    p[1]<-x2
    p[2]<-y2
  }else {
    p[1]<-x1
  p[2]<-y1
  }
  return(p)
}

gg_circle <- function(r, xc, yc, color="black", fill=NA, ...) {# reference 4
  x <- xc + r*cos(seq(0, pi, length.out=100))
  ymax <- yc + r*sin(seq(0, pi, length.out=100))
  ymin <- yc + r*sin(seq(0, -pi, length.out=100))
  annotate("ribbon", x=x, ymin=ymin, ymax=ymax, color=color, fill=fill, ...)
}



# reference 1
random_walk <- function(n.org, steps, plot = TRUE){# calculate position of points for every movement.
  
  whereto <- matrix(ncol = 2)
  t <- 180
  
  for(x in 1:n.org){
    walker <- matrix(c(0,0), nrow = 150+1, ncol = 2, byrow = T)#steps
    set.seed(312132+x)
    x0 <-runif(1,-9,9)
    set.seed(1111213+x)
    y0 <-runif(1,-9,9)
    walker[1,] <-c(x0,y0)
    for(i in 1:steps){
      set.seed(2232132+i+t)
      r <- runif(1,0,2*pi)
      set.seed(1789812+i+t)
      l <- runif(1,0,2)
      x1 = x0 + l*cos(r)
      y1 = y0 + l*sin(r)
      if(x0^2+y0^2<9){
        if(x1^2+y1^2>9){
          p <- getpoint(0,0,3,x0,y0,x1,y1)
          x1 <- p[1]
          y1 <- p[2]
        }
      } else if(round(x0^2+y0^2,3)==9) {
        if(x1^2+y1^2>9){
          x1 <- x0
          y1 <- y0
        }
      }
      walker[i+1,1] <- x1
      walker[i+1,2] <- y1
      x0 <- x1
      y0 <- y1
    }
    t= t+180
    whereto <- rbind(whereto, walker)
  }
  id <- rep(1:n.org, each = 151)
  colnames(whereto) <- c("x" , "y")
  whereto <- as.data.frame(whereto)
  whereto <- cbind(whereto[2:nrow(whereto),], org = factor(id))
  if(plot){
    require(ggplot2)
    p <- ggplot(whereto, aes(x = x, y = y,colour = org))
    p <- p + annotate("path",x=0+3*cos(seq(0,2*pi,length.out=100)),y=0+3*sin(seq(0,2*pi,length.out=100)))+
      gg_circle(r=3, xc=0, yc=0, color="black", fill="orange",alpha=0.7) +
      geom_path(size=0.2)+
      xlim(-10,10)+
      ylim(-10,10)+
      annotate(geom = "text",x = 0, y = 0,label = "SUGAR")
    print(p)
  }
  return(whereto)
}

rw.test <- random_walk(20, 150)

rw.test$step <- rep(1:151,20)

# reference 5
p <- ggplot(rw.test,
            mapping = aes(x = x, y=y, colour = org))+
  gg_circle(r=3, xc=0, yc=0, color="black", fill="orange",alpha=0.7) +
  annotate(geom = "text",x = 0, y = 0,label = "SUGAR") +
  geom_text(rw.test,mapping =  aes(x = x, y=y,colour = org, label = emoji("alien")),family="EmojiOne",size=12)+
  geom_path(rw.test,
            mapping = aes(x = x, y=y, colour = org),alpha = 0.3)+
  xlim(-10,10)+
  ylim(-10,10)
pp <- p + theme_bw()+ transition_reveal(step)
animate(pp, fps=2)

Reference

Borrelli, Jon. 2014. “Random walks in R.” https://assemblingnetwork.wordpress.com/2014/03/24/random-walks-in-r/.

inscaven. 2017. “sort using R language.” https://stackoverrun.com/cn/q/9939436.

Liu, Yugang. 2017. “R calculate intersection point between line and circle.” https://blog.csdn.net/BaiHuaXiu123/article/details/54564208.

Xiao, Zi. 2016. “plot a circle in ggplot.” https://qa.1r1g.com/sf/ask/480391971/.

You, Guangchuang. 2019. “An Introduction to emojifont package.” https://cran.r-project.org/web/packages/emojifont/vignettes/emojifont.html.